#!/usr/bin/python2.5
#
# Copyright 2009 Roman Nurik
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Defines common geo math functions used throughout the library."""

__author__ = 'api.roman.public@gmail.com (Roman Nurik)'

import math

import geotypes

RADIUS = 6378135


def distance(p1, p2):
  """Calculates the great circle distance between two points (law of cosines).

  Args:
    p1: A geotypes.Point or db.GeoPt indicating the first point.
    p2: A geotypes.Point or db.GeoPt indicating the second point.

  Returns:
    The 2D great-circle distance between the two given points, in meters.
  """
  p1lat, p1lon = math.radians(p1.lat), math.radians(p1.lon)
  p2lat, p2lon = math.radians(p2.lat), math.radians(p2.lon)
  # work out the internal value for the spherical law of cosines and clamp
  # it between -1.0 and 1.0 to avoid python rounding errors
  sloc = (math.sin(p1lat) * math.sin(p2lat) +
          math.cos(p1lat) * math.cos(p2lat) * math.cos(p2lon - p1lon))
  sloc = max(min(sloc, 1.0), -1.0)
  return RADIUS * math.acos(sloc)

def distance2lat(distance):
    return distance * 180.0 / RADIUS

def distance2lon(p1, distance):
    p1lat, p1lon = math.radians(p1.lat), math.radians(p1.lon)
    dis_per_lon = RADIUS / 180.0 * math.cos(p1lat)
    return distance/dis_per_lon

def adjust_lat(lat):
    if lat > 90:
        return 90
    if lat < -90:
        return -90
    return lat

def adjust_lon(lon):
    if lon > 180:
        return 180
    if lon < -180:
        return -180
    return lon

def box_around(center, distance):
    center_lat, center_lon = center.lat, center.lon
    dis_lat, dis_lon = distance2lat(distance), distance2lon(center, distance)
    return geotypes.Box(adjust_lat(center_lat+dis_lat), adjust_lon(center_lon+dis_lon),
        adjust_lat(center_lat-dis_lat), adjust_lon(center_lon-dis_lon))